#include "scr_list.h"
#include "lvgl.h"
#include <stdlib.h>

typedef struct Node {
  setup_func_t setup;
  struct Node *next;
} Node;

Node *global_head = NULL; // 全局链表头指针

Node *create_node(setup_func_t setup) {
  Node *new_node = (Node *)malloc(sizeof(Node));
  if (new_node == NULL) {
    LV_LOG_ERROR("Memory allocate failed\n");
    exit(1);
  }
  new_node->setup = setup;
  new_node->next = NULL;
  return new_node;
}

void insert_at_head(setup_func_t setup) {
  Node *new_node = create_node(setup);
  if (global_head == NULL) {
    global_head = new_node;
  } else {
    new_node->next = global_head;
    global_head = new_node;
  }
}

Node *find_node(setup_func_t setup) {
  Node *current_scr = global_head;
  while (current_scr != NULL && current_scr->setup != setup) {
    current_scr = current_scr->next;
  }
  return current_scr;
}

void push_scr(setup_func_t setup) { insert_at_head(setup); }

setup_func_t pop_scr() {
  if (global_head == NULL) {
    return NULL;
  }
  setup_func_t setup = global_head->setup;
  Node *temp = global_head;
  global_head = global_head->next;
  free(temp);
  return setup;
}

setup_func_t current_scr() {
  if (global_head == NULL) {
    return NULL;
  } else {
    return global_head->setup;
  }
}

uint32_t count_scr() {
  uint32_t count_scr = 0;
  Node *current_scr = global_head;
  while (current_scr != NULL) {
    count_scr++;
    current_scr = current_scr->next;
  }

  return count_scr;
}
